const Router = require('@koa/router');
const { Ticket, User } = require('../databases');

const ticketRouter = new Router({
    prefix: '/ticket'
});

// 获取门票首页
ticketRouter.get('/', (ctx, next) => {
    ctx.body = {
        code: 200,
        message: 'Ticket home page',
        data: 'Welcome to ticket API',
    };
});

// 添加门票
ticketRouter.post('/add', async (ctx, next) => {
    try {
        const { uid, jid, name, desc, content } = ctx.request.body;
        
        // 验证用户是否存在
        const user = await User.findByPk(uid);
        if (!user) {
            ctx.body = {
                code: 404,
                message: '用户不存在',
                data: null
            };
            return;
        }

        // 验证景点是否存在
        const jingdian = await Jingdian.findByPk(jid);
        if (!jingdian) {
            ctx.body = {
                code: 404,
                message: '景点不存在',
                data: null
            };
            return;
        }

        const currentTicket = await Ticket.create({
            uid,
            jid,
            name,
            desc,
            content
        });

        ctx.body = {
            code: 200,
            message: '添加门票成功',
            data: currentTicket,
        };
    } catch (error) {
        ctx.body = {
            code: 500,
            message: '添加门票失败',
            error: error.message
        };
    }
});

// 获取所有门票列表
ticketRouter.get('/list', async (ctx, next) => {
    try {
        const tickets = await Ticket.findAll({
            include: [
                {
                    model: User,
                    as: 'user',
                    attributes: ['uid', 'username']
                },
                {
                    model: Jingdian,
                    as: 'jingdian',
                    attributes: ['jid', 'name', 'desc']
                }
            ],
            order: [['create', 'DESC']]
        });
        
        ctx.body = {
            code: 200,
            message: '获取门票列表成功',
            data: tickets,
        };
    } catch (error) {
        ctx.body = {
            code: 500,
            message: '获取门票列表失败',
            error: error.message
        };
    }
});

// 根据ID获取门票详情
ticketRouter.get('/:id', async (ctx, next) => {
    try {
        const { id } = ctx.params;
        const ticket = await Ticket.findByPk(id, {
            include: [
                {
                    model: User,
                    as: 'user',
                    attributes: ['uid', 'username']
                },
                {
                    model: Jingdian,
                    as: 'jingdian',
                    attributes: ['jid', 'name', 'desc', 'logoRemoteImageUrl']
                }
            ]
        });

        if (!ticket) {
            ctx.body = {
                code: 404,
                message: '门票不存在',
                data: null
            };
            return;
        }

        ctx.body = {
            code: 200,
            message: '获取门票详情成功',
            data: ticket,
        };
    } catch (error) {
        ctx.body = {
            code: 500,
            message: '获取门票详情失败',
            error: error.message
        };
    }
});

// 更新门票信息
ticketRouter.put('/:id', async (ctx, next) => {
    try {
        const { id } = ctx.params;
        const { name, desc, content } = ctx.request.body;

        const ticket = await Ticket.findByPk(id);
        if (!ticket) {
            ctx.body = {
                code: 404,
                message: '门票不存在',
                data: null
            };
            return;
        }

        await ticket.update({
            name: name || ticket.name,
            desc: desc || ticket.desc,
            content: content || ticket.content
        });

        ctx.body = {
            code: 200,
            message: '更新门票成功',
            data: ticket,
        };
    } catch (error) {
        ctx.body = {
            code: 500,
            message: '更新门票失败',
            error: error.message
        };
    }
});

// 删除门票
ticketRouter.delete('/:id', async (ctx, next) => {
    try {
        const { id } = ctx.params;
        
        const ticket = await Ticket.findByPk(id);
        if (!ticket) {
            ctx.body = {
                code: 404,
                message: '门票不存在',
                data: null
            };
            return;
        }

        await ticket.destroy();

        ctx.body = {
            code: 200,
            message: '删除门票成功',
            data: null,
        };
    } catch (error) {
        ctx.body = {
            code: 500,
            message: '删除门票失败',
            error: error.message
        };
    }
});

// 根据用户ID获取门票列表
ticketRouter.get('/user/:userId', async (ctx, next) => {
    try {
        const { userId } = ctx.params;
        
        const user = await User.findByPk(userId);
        if (!user) {
            ctx.body = {
                code: 404,
                message: '用户不存在',
                data: null
            };
            return;
        }

        const tickets = await Ticket.findAll({
            where: { uid: userId },
            include: [
                {
                    model: User,
                    as: 'user',
                    attributes: ['uid', 'username']
                },
                {
                    model: Jingdian,
                    as: 'jingdian',
                    attributes: ['jid', 'name', 'desc']
                }
            ],
            order: [['create', 'DESC']]
        });

        ctx.body = {
            code: 200,
            message: '获取用户门票列表成功',
            data: tickets,
        };
    } catch (error) {
        ctx.body = {
            code: 500,
            message: '获取用户门票列表失败',
            error: error.message
        };
    }
});

// 根据景点ID获取门票列表
ticketRouter.get('/jingdian/:jid', async (ctx, next) => {
    try {
        const { jid } = ctx.params;
        
        const jingdian = await Jingdian.findByPk(jid);
        if (!jingdian) {
            ctx.body = {
                code: 404,
                message: '景点不存在',
                data: null
            };
            return;
        }

        const tickets = await Ticket.findAll({
            where: { jid },
            include: [
                {
                    model: User,
                    as: 'user',
                    attributes: ['uid', 'username']
                },
                {
                    model: Jingdian,
                    as: 'jingdian',
                    attributes: ['jid', 'name', 'desc']
                }
            ],
            order: [['create', 'DESC']]
        });

        ctx.body = {
            code: 200,
            message: '获取景点门票列表成功',
            data: tickets,
        };
    } catch (error) {
        ctx.body = {
            code: 500,
            message: '获取景点门票列表失败',
            error: error.message
        };
    }
});

module.exports = ticketRouter;
